/******************************************************************************

  Copyright (C), 2012-2016, ZHANGYA

 ******************************************************************************
  File Name     : uaprot.h
  Version       : Initial Draft
  Author        : zhangya
  Created       : 2024/5/7
  Last Modified :
  Description   : uaprot.h
  Function List :
  History       :
  1.Date        : 2024/5/7
    Author      : zhangya
    Modification: Created file

******************************************************************************/

#ifndef _UAPROT_H_
#define _UAPROT_H_

#include "main.h"

#ifdef __cplusplus
extern "C" {
#endif

/*--------------------------------------------------------------------*
 * $external variables                           *
 *--------------------------------------------------------------------*/
#define XDPACK_DATA_LENGTH              (0x0100)

/*--------------------------------------------------------------------*
 * $external routine prototypes                  *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $internal routine prototypes                  *
 *--------------------------------------------------------------------*/
 //bctl
enum keyid_e{
    //for bit0~5 bctl key
    KEYID_BCTL_IDLE = 0,
    KEYID_BCTL_VERSION = 1,     //for query version string
    KEYID_BCTL_WKSTAT = 2,	    //indicate work status change --> uaprot_wkstat_t --> for keep alive
    KEYID_BCTL_WKVIEW = 3,	    //query video input work status --> uaprot_wkview_t
    KEYID_BCTL_ADJUST = 4,	    //query or set --> video_adjust_t
    KEYID_BCTL_DOPEN = 5,       //start data translate request
    KEYID_BCTL_DCLOSE = 6,	    //close data translate request
    KEYID_BCTL_DATA = 8,        //when data translate --> offt = {24bit -> offt : 00}
    KEYID_BCTL_KSTRING = 9,     //just for debug

    KEYID_BCTL_PBCTL   = 0x20,	//for platform board control command -> <prot = {reboot}>
    KEYID_BCTL_EVENT   = 0x21,	//send event
    KEYID_BCTL_WKMODE  = 0x22,	//control for work mode
    KEYID_BCTL_VFORMAT = 0x23,	//for video format <prot = { }>
    KEYID_BCTL_VINPORT = 0x24,	//for video input  port select for <prot = {hdmi, sdi, lvds, =get(input)}>
    KEYID_BCTL_VOUTPRT = 0x25,	//for video output port select <prot = {hdmi, rkcore, none, =get(output)}>

    KEYID_BCTL_ERROR  = 0xFF,
};

//------prot
enum restart_e{
    RESTART_OUTPUT        = 1,
    RESTART_INPUT         = 2,
    RESTART_TOTAL         = 3
};

enum video_e{
    VIDEO_UPDATE          = 4,
    VIDEO_RESTART         = 5
};

enum store_e{
    STORE_BASIC           = 0x10,
    STORE_VIDEO           = 0x11
};

enum switch_e{
    SWITCH_2160P          = 0x20,
    SWITCH_1080P          = 0x21
};

enum system_e{
    SYSTEM_RELOAD          = 0x46,
    SYSTEM_REBOOT          = 0x48,
    SYSTEM_UPGRADE         = 0x5A
};

enum rkctl_e{
    RKCORE_5VCTL_OFF        = 0x30,
    RKCORE_5VCTL_ON         = 0x31,
    RKCORE_HPCTL_OFF        = 0x32,
    RKCORE_HPCTL_ON         = 0x33
};

enum pinmux_e{
    PINMUX_HDMI  = 0,
    PINMUX_SDI,
    PINMUX_LVDS,
    PINMUX_PHYMAX
};

enum poutmux_e{
    POUTMUX_HDMI  = 0,
    POUTMUX_RKCORE,
    POUTMUX_NONE,
    POUTMUX_PHYMAX
};

enum format_e{
    FORMAT_2160P = 0,
    FORMAT_1080P,
    FORMAT_PHYMAX
};

//----mask
enum xbit_e{
    //for bit6~7 control status
    KEYID_XBIT_KCMD   = 0x00,
    KEYID_XBIT_ACK	  = 0x40,
    KEYID_XBIT_NACK   = 0x80,
    KEYID_XBIT_NERR   = 0xC0,
    KEYID_XBIT_HIMASK = 0xC0,	//hight mask for xbit
    KEYID_XBIT_LOMASK = 0x3F	//low   mask for control
};

enum dirty_e{
    DIRTY_READ            = 0x0000,
    DIRTY_BRIGHTNESS      = 0x0001,
    DIRTY_CONTRAST        = 0x0002,
    DIRTY_HUE             = 0x0004,
    DIRTY_SATURATION      = 0x0008,
    DIRTY_SHARPNESS       = 0x0010,
    DIRTY_DENOISE         = 0x0020,
    DIRTY_GAIN            = 0x0040,
    DIRTY_GFE_CLASS       = 0x0080,
    DIRTY_TOTAL           = 0x00EB
};

typedef struct pack_para_str{
	uint8_t msgid;
	uint8_t bctl;
	uint8_t port;
	uint8_t dlen;
	uint8_t * pdata;
}pack_para_t;

/*--------------------------------------------------------------------*
 * $project-wide global variables                *
 *--------------------------------------------------------------------*/
#define KSET_BITS_DIRTY(kval, bits)		((kval)|=(bits))
#define KGET_BITS_DIRTY(kval, bits)     ((kval)&(bits))

/*--------------------------------------------------------------------*
 * $module-wide global variables                 *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $constants                                    *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $macros                                       *
 *--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*
 * $routines' implementations                    *
 *--------------------------------------------------------------------*/

uint16_t combo_word(const uint8_t * buff);
void parse_word(uint8_t * buff, uint16_t word);
uint32_t combo_dword(const uint8_t * buff);
void parse_dword(uint8_t * buff, uint32_t dword);

int uaprot_package(const pack_para_t * para, uint8_t * buff, uint16_t size);
bool uaprot_control_adjust(const uint8_t * dat, uint16_t dlen, pack_para_t * para);

bool uaprot_unpack_one(pack_para_t * para, const int16_t data);

int uaprot_find(const uint8_t * pack, const uint16_t len, uint8_t * msgid);
int uaprot_unpack(pack_para_t * para, const uint8_t * pack, const uint16_t len);

int uaprot_set_gain(uint8_t * buff, uint16_t size, uint16_t r, uint16_t g, uint16_t b);
void uaprot_get_gain(const uint8_t * pack, uint16_t * pr, uint16_t * pg, uint16_t * pb);

int uaprot_set_class(uint8_t * buff, uint16_t size, uint16_t amount);
void uaprot_get_class(const uint8_t * pack, uint16_t * amount);

#ifdef __cplusplus
}
#endif

#endif  //#ifndef _UAPROT_H_

